package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.app.Dialog;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.databinding.ObservableField;

import com.whmnx.base.bean.ImgResultBean;
import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.PersonalBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.PhotoUtil;
import com.whmnx.my.R;
import com.whmnx.my.ui.fragment.MyEditInfoFragment;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/9/18 0018 16:06
 * version: 1.0
 */
public class EidtModel extends ToolbarViewModel {


    public ObservableField<String> nick = new ObservableField<>("");
    public EidtModel(@NonNull Application application) {
        super(application);
    }

    private MyEditInfoFragment activity;
    private Dialog dialogHead;

    private Uri imageUri;
    private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg");

    private File fileCropUri = new File(Environment.getExternalStorageDirectory().getPath() + "/crop_photo.jpg");
    /* 请求识别码 */
    public static final int CODE_GALLERY_REQUEST = 0xa0;
    public static final int CODE_CAMERA_REQUEST = 0xa1;
    public static final int CODE_RESULT_REQUEST = 0xa2;

    public static int OUTPUT_X = 480;
    public static int OUTPUT_Y = 480;

    public void initToolbar(MyEditInfoFragment activity) {
        setTitleText("编辑资料");
        setRightTextVisible(View.VISIBLE);
        setRightText("保存");
        this.activity = activity;
    }

    //更换图片
    public BindingCommand imgOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            showHeadDialog();
           // UItils.timeStamp2Date(System.currentTimeMillis());
            //ToastUtil.toastShortMessage(UItils.timeStamp2Date(System.currentTimeMillis()));

        }
    });

    private void showHeadDialog() {
        dialogHead = new Dialog(activity, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_head_portrait, null);
        view.findViewById(R.id.dialog_shoot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageUri = Uri.fromFile(fileUri);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    imageUri = FileProvider.getUriForFile(activity, "com.whmnx.webcasts.FileProvider", fileUri);
                }
                PhotoUtil.takePicture(activity, imageUri, CODE_CAMERA_REQUEST);
                dialogHead.dismiss();
            }
        });
        view.findViewById(R.id.dialog_photo_album).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoUtil.openPic(activity, CODE_GALLERY_REQUEST);
                dialogHead.dismiss();
            }
        });
        view.findViewById(R.id.dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogHead.dismiss();
            }
        });
        dialogHead.setContentView(view);
        Window window = dialogHead.getWindow();
        if (window == null) {
            return;
        }
     //   window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//宽高最大
        //显示对话框//设置Dialog从窗体底部弹出
        dialogHead.show();
        window.setGravity(Gravity.BOTTOM);
        //获取窗体的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置Dialog距离底部的距离
        lp.y = 20;
        //将属性设置给窗体
        window.setAttributes(lp);
    }

    @Override
    public void rightTextOnClick() {

        if (activity.files.size() > 0) {
            HttpClient.postImg(activity, Constants.UPLOADHEADIMG, activity.files, new CallBack<List<ImgResultBean>>() {
                @Override
                public void onSuccess(List<ImgResultBean> baseBean) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
                    params.put("img", baseBean.get(0).shortPath);
                    HttpClient.get(activity, Constants.UPDATEHEADIMAGE, params, new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            RxBus.getDefault().post(EventConstants.IMG);
                            finish();
                        }
                    });
                }
            });
        }

        if (!TextUtils.isEmpty(nick.get())){
            Map<String, Object> params = new HashMap<>();
            params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
            params.put("nick",nick.get());
            HttpClient.get(activity, Constants.UPDATEUSERNICK, params,new CallBack<PersonalBean>() {
                @Override
                public void onSuccess(PersonalBean personalBean) {
                    RxBus.getDefault().post(EventConstants.IMG);
                    finish();
                }
            });
        }
    }
}
